package com.controller.home;

import com.controller.BaseController;
import com.dao.UserAddressMapper;
import com.entity.Goods;
import com.entity.ShopOrderGoods;
import com.entity.UserAddress;
import com.entity.UserCartLog;
import com.service.imple.home.CartImple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**购物车页面**/
@Controller
@RequestMapping("home/cart")
public class UserCartLogController extends BaseController<CartImple> {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @RequestMapping(method = RequestMethod.GET)
    public String ServletCart(@SessionAttribute("userId") Integer userId, ModelMap modelMap){
        Map<String,Object> map = new HashMap();
        map.put("userId",userId);
        List<UserCartLog> list= BaseService.select(map);

        for (UserCartLog m:list) {
            System.out.println(m);
        }
        float totalPrice = 0;
        for (UserCartLog cart : list) {
            totalPrice += cart.getSellPrice() * cart.getGoNum();
        }
        modelMap.put("list" ,list);
        modelMap.put("totalPrice", totalPrice);

        return "homepage/cart";
    }

    /**
     * 添加购物车判断是否有相同的物品，有则加一
     * @param goodsId
     * @param userId
     * @param gonum
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insert(Integer goodsId, @SessionAttribute("userId")Integer userId, @RequestParam(value="id",required=true,defaultValue="1")Integer gonum){
        Map<String,Object> map = new HashMap<>();
        UserCartLog userCartLog = new UserCartLog();
        userCartLog.setUserId(userId);
        userCartLog.setGoodsId(goodsId);
        userCartLog.setGoNum(gonum);
        userCartLog.setAddTime(new Date());
        map.put("goodsId",goodsId);
        List<UserCartLog> list = BaseService.SelectAll(map);
        if (list.size()!=0){
            list.get(0).setGoNum(list.get(0).getGoNum()+1);
            int count = BaseService.update(list.get(0));
            if (count>=1){
                map.put("status",1);
            }else {
                map.put("status",2);
            }
        }else {
            int count = BaseService.insert(userCartLog);
            if (count>=1){
                map.put("status",1);
            }else {
                map.put("status",2);
            }
        }
        return map;
    }

    @RequestMapping(method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> DeleteCart(Integer id){
        Map<String,Object> map = new HashMap<>();
        int count = BaseService.delete(id);
        if (count>=1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("delete")
    public String Delete(Integer id){
        BaseService.delete(id);
        return "home/cart";
    }

    //立即购买
    @RequestMapping("toWriteorder")
    public String toWriteorder(@SessionAttribute("userId")Integer userId,ModelMap modelMap){
        Map<String,Object> map = new HashMap();
        map.put("userId",userId);
        List<UserCartLog> list = BaseService.select(map);
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> last= userAddressMapper.Select(userAddress);
        Integer totalPrice = list.get(0).getSellPrice();
        modelMap.put("totalPrice", totalPrice);
        modelMap.put("list",list);
        modelMap.put("last",last);
        return "homepage/writeorder";
    }

    //清空购物车信息
    @RequestMapping("EmptyCart")
    public String EmptyCart(@SessionAttribute("userId")Integer userId){
        return "redirect:/home/pay/pay";
    }

    //增加购物车商品数量
    @RequestMapping("addGoodsNum")
    @ResponseBody
    public Map<String,Object> addGoodsNum(@SessionAttribute("userId")Integer userId,Integer id,Integer number){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<UserCartLog> list = BaseService.selectId(map);
        list.get(0).setGoNum(number);
        int count = BaseService.update(list.get(0));
        if (count>=1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }
}
